-module(kcpserver).
-on_load(initSo/0).
-export([
init/1,
initSo/0,
createKcpServer/1,
handle_call/3,
handle_cast/2,
handle_info/2,
test/0,
updateCpp/0,
receiveUdp/3,
getUDPPackets/0,
getKCPPackets/0,
sendData/3,
closeAgent/1,
getClosedAgent/0
	]).

initSo() ->
	erlang:load_nif("libtest1", 0).

createKcpServer(Port)->
	gen_server:start({local, kcpserver}, ?MODULE, Port, []).

init(Port) ->
	test(),
	{ok, Socket} = gen_udp:open(Port),
	ets:new(server, [set, private, named_table]),
	ets:insert(server, {udpServer, Socket}),
	runUpdate(),
	{ok, null}.


handle_cast(Message, LoopData) ->
	io:format("~s~n", [Message]),
	{noreply, LoopData}.

handle_call(Message, From, LoopData) ->
	{noreply, LoopData}.


handle_info(update, State) ->
	updateProc(),
	Timer = erlang:send_after(1000, self(), update),
	{noreply, Timer};

handle_info(Info, LoopData) ->
	io:format("~w:~w~n", [Info, LoopData]),
	{udp, Socket, Ip, Port, Data} = Info,
	%Agents = element(2,lists:nth(1, ets:lookup(server, copyAgents))),
	%lists:keyfind()
	IpStr = lists:flatten(io_lib:format("~w", [Ip])),
	PortStr = lists:flatten(io_lib:format("~w", [Port])),
	DataBit = erlang:list_to_bitstring(Data),
	receiveUdp(IpStr, PortStr, DataBit),
	{noreply, LoopData}.

receiveUdp(Ip, Port, Data) ->
	exit(nif_library_not_loaded).

% 2000 连接
updateProc() ->
	Ip = lists:flatten(io_lib:format("~w", [{127,0,0,1}])),
	Port = lists:flatten(io_lib:format("~w", [5050])),
	sendData(Ip, Port, <<"HelloWorldGood">>),
	updateCpp(),
	UDPPackets = getUDPPackets(),
	KCPPackets = getKCPPackets(),
	io:format("~nUDPPackets:~w~n", [UDPPackets]),
	io:format("~nKCPPacket~w~n", [KCPPackets]),
	sendUdpData(UDPPackets),
	receiveKcpPacket(KCPPackets),
	Closed = getClosedAgent(),
	io:format("~nCloseAgent~w~n", [Closed]),
	CPort = lists:flatten(io_lib:format("~w~w", [{127,0,0,1}, 5050])),
	closeAgent(CPort).

receiveKcpPacket(KCPPackets) ->
	A=1.

sendUdpData([]) ->
	undefined;

sendUdpData([H | T]) ->
	IpPort = H,
	sendUdpData(IpPort, T).

sendUdpData(IpPort, [H | T]) ->
	Data = H,
	Socket = element(2,lists:nth(1, ets:lookup(server, udpServer))),

	IpPort1 = erl_scan:string(IpPort),
	FIpOnePort = lists:filter(fun({integer, _, _}) -> true; (_) ->false end, element(2, IpPort1)),
	Ip = lists:map(fun({_,_,V}) -> V end,  lists:sublist(FIpOnePort, 4)),
	Port = lists:nth(1,  lists:map(fun({_,_,V}) -> V end,  lists:sublist(FIpOnePort, 5, 1))),
	Ip2 = {lists:nth(1, Ip), lists:nth(2, Ip), lists:nth(3, Ip), lists:nth(4, Ip)},
	case gen_udp:send(Socket, Ip2, Port, Data) of
		ok ->
			undefined;
		{error, _} ->
			undefined
	end,

	sendUdpData(T).


getUDPPackets()->
	exit(nif_library_not_loaded).

getKCPPackets() ->
	exit(nif_library_not_loaded).

updateCpp()->
	exit(nif_library_not_loaded).

test() ->
	exit(nif_library_not_loaded).

sendData(Ip, Port, Data) ->
	exit(nif_library_not_loaded).

closeAgent(UdpPort) ->
	exit(nif_library_not_loaded).

getClosedAgent() ->
	exit(nif_library_not_loaded).


runUpdate() ->
	erlang:send_after(1000, self(), update).


getNow() ->
	erlang:system_time()/1000000000.0.